package hmis.pos.Service.ServiceImpl;


import hmis.pos.Base.BaseService.Impl.BaseServiceImpl;
import hmis.pos.Entity.RoomPrice;
import hmis.pos.Entity.RoomType;
import hmis.pos.Repository.RoomMessageRepository;
import hmis.pos.Entity.RoomMessage;
import hmis.pos.Repository.RoomPriceRepository;
import hmis.pos.Repository.RoomTypeRepository;
import hmis.pos.Service.RoomMessageService;
import hmis.pos.Service.RoomPriceService;
import hmis.pos.Service.RoomTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class RoomMessageServiceImpl extends BaseServiceImpl<RoomMessage,RoomMessageRepository> implements RoomMessageService {

    @Autowired
    private RoomMessageRepository roomMessageRepository;

    @Autowired
    private RoomTypeService roomTypeService;

    @Autowired
    private RoomPriceRepository roomPriceRepository;

    @Autowired
    private RoomPriceService roomPriceService;

    @Override
    public List<RoomMessage> findByType(String type) {
        return roomMessageRepository.findByType(type);
    }

    @Override
    public RoomMessage save(RoomMessage roomMessage){

        if(roomMessageRepository.findByRoomid(roomMessage.getRoomid())!=null)
            return null;

        List<RoomPrice> roomPriceList = roomPriceRepository.findAfterToday(roomMessage.getType());

        roomMessage.setStatus("空闲");

        RoomType roomType = roomTypeService.findByType(roomMessage.getType());
        roomMessageRepository.save(roomMessage);
        int roomnumber = roomMessageRepository.countByType(roomMessage.getType());
        roomType.setRoomnumber(roomnumber);
        roomTypeService.update(roomType);

        //修改价格日历的可预订数
        for(RoomPrice roomPrice : roomPriceList){
            roomPrice.setSurplus(roomPrice.getSurplus()+1);
            roomPriceService.update(roomPrice);
        }

        return roomMessage;
    }

    @Override
    public boolean delete(RoomMessage roomMessage){
        if(roomMessageRepository.findById(roomMessage.getId())==null)
            return false;

        List<RoomPrice> roomPriceList = roomPriceRepository.findAfterToday(roomMessage.getType());


        roomMessageRepository.delete(roomMessage);
        int roomnumber = roomMessageRepository.countByType(roomMessage.getType());
        RoomType roomType = roomMessage.getRoomType();
        roomType.setRoomnumber(roomnumber);
        roomTypeService.update(roomType);

        //修改价格日历的可预订数
        for(RoomPrice roomPrice : roomPriceList){
            int number = roomPrice.getSurplus()-1;
            if(number<0)number=0;
            roomPrice.setSurplus(number);
            roomPriceService.update(roomPrice);
        }

        return true;
    }

    @Override
    public RoomMessage update(RoomMessage roomMessage){
        if(roomMessageRepository.findById(roomMessage.getId())==null)
            return null;
        RoomMessage preRoomMessage = roomMessageRepository.findById(roomMessage.getId()).get();
        RoomType preRoomType = preRoomMessage.getRoomType();
        RoomType roomType = roomTypeService.findByType(roomMessage.getType());
        int preRoomid = preRoomMessage.getRoomid();
        int roomid = roomMessage.getRoomid();
        String preType = preRoomMessage.getType();
        String type = roomMessage.getType();

        if(preRoomid!=roomid&&(roomMessageRepository.findByRoomid(roomid)!=null))
                return null;
        roomMessageRepository.save(roomMessage);
        int preNumber = roomMessageRepository.countByType(preType);
        int roomNumber = roomMessageRepository.countByType(type);

        preRoomType.setRoomnumber(preNumber);
        roomType.setRoomnumber(roomNumber);
        roomMessage.setRoomType(roomType);
        roomTypeService.update(preRoomType);
        roomTypeService.update(roomType);
        return roomMessage;
    }

    @Override
    public RoomMessage findByRoomid(int roomid) {
        return roomMessageRepository.findByRoomid(roomid);
    }

}
